package com.caine.multithread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: CaineZhu
 * @Description:
 * @Date: Created in 17:07 2020/12/10
 * @Modified By:
 */
public class ConditionDemo {
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println("t1 run");
            lock.lock();
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println("t1 exit");
            }
        }).start();

        new Thread(() -> {
            System.out.println("t2 run");
            lock.lock();
            try {
                condition.signal();
            } finally {
                lock.unlock();
                System.out.println("t2 exit");
            }
        }).start();
    }
}
